home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / apps / service.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  17.9 KB  |  488 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'tmatsuo@sios.com (Takashi MATSUO)'
  5.  
  6. try:
  7.     from xml.etree import cElementTree as ElementTree
  8. except ImportError:
  9.     
  10.     try:
  11.         import cElementTree as ElementTree
  12.     except ImportError:
  13.         
  14.         try:
  15.             from xml.etree import ElementTree
  16.         except ImportError:
  17.             from elementtree import ElementTree
  18.  
  19.  
  20.  
  21. import urllib
  22. import gdata
  23. import atom.service as atom
  24. import gdata.service as gdata
  25. import gdata.apps as gdata
  26. import atom
  27. API_VER = '2.0'
  28. HTTP_OK = 200
  29. UNKOWN_ERROR = 1000
  30. USER_DELETED_RECENTLY = 1100
  31. USER_SUSPENDED = 1101
  32. DOMAIN_USER_LIMIT_EXCEEDED = 1200
  33. DOMAIN_ALIAS_LIMIT_EXCEEDED = 1201
  34. DOMAIN_SUSPENDED = 1202
  35. DOMAIN_FEATURE_UNAVAILABLE = 1203
  36. ENTITY_EXISTS = 1300
  37. ENTITY_DOES_NOT_EXIST = 1301
  38. ENTITY_NAME_IS_RESERVED = 1302
  39. ENTITY_NAME_NOT_VALID = 1303
  40. INVALID_GIVEN_NAME = 1400
  41. INVALID_FAMILY_NAME = 1401
  42. INVALID_PASSWORD = 1402
  43. INVALID_USERNAME = 1403
  44. INVALID_HASH_FUNCTION_NAME = 1404
  45. INVALID_HASH_DIGGEST_LENGTH = 1405
  46. INVALID_EMAIL_ADDRESS = 1406
  47. INVALID_QUERY_PARAMETER_VALUE = 1407
  48. TOO_MANY_RECIPIENTS_ON_EMAIL_LIST = 1500
  49. DEFAULT_QUOTA_LIMIT = '2048'
  50.  
  51. class Error(Exception):
  52.     pass
  53.  
  54.  
  55. class AppsForYourDomainException(Error):
  56.     
  57.     def __init__(self, response):
  58.         Error.__init__(self, response)
  59.         
  60.         try:
  61.             self.element_tree = ElementTree.fromstring(response['body'])
  62.             self.error_code = int(self.element_tree[0].attrib['errorCode'])
  63.             self.reason = self.element_tree[0].attrib['reason']
  64.             self.invalidInput = self.element_tree[0].attrib['invalidInput']
  65.         except:
  66.             self.error_code = UNKOWN_ERROR
  67.  
  68.  
  69.  
  70.  
  71. class AppsService(gdata.service.GDataService):
  72.     '''Client for the Google Apps Provisioning service.'''
  73.     
  74.     def __init__(self, email = None, password = None, domain = None, source = None, server = 'apps-apis.google.com', additional_headers = None, **kwargs):
  75.         """Creates a client for the Google Apps Provisioning service.
  76.  
  77.     Args:
  78.       email: string (optional) The user's email address, used for
  79.           authentication.
  80.       password: string (optional) The user's password.
  81.       domain: string (optional) The Google Apps domain name.
  82.       source: string (optional) The name of the user's application.
  83.       server: string (optional) The name of the server to which a connection
  84.           will be opened. Default value: 'apps-apis.google.com'.
  85.       **kwargs: The other parameters to pass to gdata.service.GDataService
  86.           constructor.
  87.     """
  88.         gdata.service.GDataService.__init__(self, email = email, password = password, service = 'apps', source = source, server = server, additional_headers = additional_headers, **kwargs)
  89.         self.ssl = True
  90.         self.port = 443
  91.         self.domain = domain
  92.  
  93.     
  94.     def _baseURL(self):
  95.         return '/a/feeds/%s' % self.domain
  96.  
  97.     
  98.     def GetGeneratorFromLinkFinder(self, link_finder, func):
  99.         '''returns a generator for pagination'''
  100.         yield link_finder
  101.         next = link_finder.GetNextLink()
  102.         while next is not None:
  103.             next_feed = func(str(self.Get(next.href)))
  104.             yield next_feed
  105.             next = next_feed.GetNextLink()
  106.  
  107.     
  108.     def AddAllElementsFromAllPages(self, link_finder, func):
  109.         '''retrieve all pages and add all elements'''
  110.         next = link_finder.GetNextLink()
  111.         while next is not None:
  112.             next_feed = func(str(self.Get(next.href)))
  113.             for a_entry in next_feed.entry:
  114.                 link_finder.entry.append(a_entry)
  115.             
  116.             next = next_feed.GetNextLink()
  117.         return link_finder
  118.  
  119.     
  120.     def RetrievePageOfEmailLists(self, start_email_list_name = None):
  121.         '''Retrieve one page of email list'''
  122.         uri = '%s/emailList/%s' % (self._baseURL(), API_VER)
  123.         if start_email_list_name is not None:
  124.             uri += '?startEmailListName=%s' % start_email_list_name
  125.         
  126.         
  127.         try:
  128.             return gdata.apps.EmailListFeedFromString(str(self.Get(uri)))
  129.         except gdata.service.RequestError:
  130.             e = None
  131.             raise AppsForYourDomainException(e.args[0])
  132.  
  133.  
  134.     
  135.     def RetrieveAllEmailLists(self):
  136.         '''Retrieve all email list of a domain.'''
  137.         ret = self.RetrievePageOfEmailLists()
  138.         return self.AddAllElementsFromAllPages(ret, gdata.apps.EmailListFeedFromString)
  139.  
  140.     
  141.     def RetrieveEmailList(self, list_name):
  142.         """Retreive a single email list by the list's name."""
  143.         uri = '%s/emailList/%s/%s' % (self._baseURL(), API_VER, list_name)
  144.         
  145.         try:
  146.             return self.Get(uri, converter = gdata.apps.EmailListEntryFromString)
  147.         except gdata.service.RequestError:
  148.             e = None
  149.             raise AppsForYourDomainException(e.args[0])
  150.  
  151.  
  152.     
  153.     def RetrieveEmailLists(self, recipient):
  154.         '''Retrieve All Email List Subscriptions for an Email Address.'''
  155.         uri = '%s/emailList/%s?recipient=%s' % (self._baseURL(), API_VER, recipient)
  156.         
  157.         try:
  158.             ret = gdata.apps.EmailListFeedFromString(str(self.Get(uri)))
  159.         except gdata.service.RequestError:
  160.             e = None
  161.             raise AppsForYourDomainException(e.args[0])
  162.  
  163.         return self.AddAllElementsFromAllPages(ret, gdata.apps.EmailListFeedFromString)
  164.  
  165.     
  166.     def RemoveRecipientFromEmailList(self, recipient, list_name):
  167.         '''Remove recipient from email list.'''
  168.         uri = '%s/emailList/%s/%s/recipient/%s' % (self._baseURL(), API_VER, list_name, recipient)
  169.         
  170.         try:
  171.             self.Delete(uri)
  172.         except gdata.service.RequestError:
  173.             e = None
  174.             raise AppsForYourDomainException(e.args[0])
  175.  
  176.  
  177.     
  178.     def RetrievePageOfRecipients(self, list_name, start_recipient = None):
  179.         '''Retrieve one page of recipient of an email list. '''
  180.         uri = '%s/emailList/%s/%s/recipient' % (self._baseURL(), API_VER, list_name)
  181.         if start_recipient is not None:
  182.             uri += '?startRecipient=%s' % start_recipient
  183.         
  184.         
  185.         try:
  186.             return gdata.apps.EmailListRecipientFeedFromString(str(self.Get(uri)))
  187.         except gdata.service.RequestError:
  188.             e = None
  189.             raise AppsForYourDomainException(e.args[0])
  190.  
  191.  
  192.     
  193.     def RetrieveAllRecipients(self, list_name):
  194.         '''Retrieve all recipient of an email list.'''
  195.         ret = self.RetrievePageOfRecipients(list_name)
  196.         return self.AddAllElementsFromAllPages(ret, gdata.apps.EmailListRecipientFeedFromString)
  197.  
  198.     
  199.     def AddRecipientToEmailList(self, recipient, list_name):
  200.         '''Add a recipient to a email list.'''
  201.         uri = '%s/emailList/%s/%s/recipient' % (self._baseURL(), API_VER, list_name)
  202.         recipient_entry = gdata.apps.EmailListRecipientEntry()
  203.         recipient_entry.who = gdata.apps.Who(email = recipient)
  204.         
  205.         try:
  206.             return gdata.apps.EmailListRecipientEntryFromString(str(self.Post(recipient_entry, uri)))
  207.         except gdata.service.RequestError:
  208.             e = None
  209.             raise AppsForYourDomainException(e.args[0])
  210.  
  211.  
  212.     
  213.     def DeleteEmailList(self, list_name):
  214.         '''Delete a email list'''
  215.         uri = '%s/emailList/%s/%s' % (self._baseURL(), API_VER, list_name)
  216.         
  217.         try:
  218.             self.Delete(uri)
  219.         except gdata.service.RequestError:
  220.             e = None
  221.             raise AppsForYourDomainException(e.args[0])
  222.  
  223.  
  224.     
  225.     def CreateEmailList(self, list_name):
  226.         '''Create a email list. '''
  227.         uri = '%s/emailList/%s' % (self._baseURL(), API_VER)
  228.         email_list_entry = gdata.apps.EmailListEntry()
  229.         email_list_entry.email_list = gdata.apps.EmailList(name = list_name)
  230.         
  231.         try:
  232.             return gdata.apps.EmailListEntryFromString(str(self.Post(email_list_entry, uri)))
  233.         except gdata.service.RequestError:
  234.             e = None
  235.             raise AppsForYourDomainException(e.args[0])
  236.  
  237.  
  238.     
  239.     def DeleteNickname(self, nickname):
  240.         '''Delete a nickname'''
  241.         uri = '%s/nickname/%s/%s' % (self._baseURL(), API_VER, nickname)
  242.         
  243.         try:
  244.             self.Delete(uri)
  245.         except gdata.service.RequestError:
  246.             e = None
  247.             raise AppsForYourDomainException(e.args[0])
  248.  
  249.  
  250.     
  251.     def RetrievePageOfNicknames(self, start_nickname = None):
  252.         '''Retrieve one page of nicknames in the domain'''
  253.         uri = '%s/nickname/%s' % (self._baseURL(), API_VER)
  254.         if start_nickname is not None:
  255.             uri += '?startNickname=%s' % start_nickname
  256.         
  257.         
  258.         try:
  259.             return gdata.apps.NicknameFeedFromString(str(self.Get(uri)))
  260.         except gdata.service.RequestError:
  261.             e = None
  262.             raise AppsForYourDomainException(e.args[0])
  263.  
  264.  
  265.     
  266.     def RetrieveAllNicknames(self):
  267.         '''Retrieve all nicknames in the domain'''
  268.         ret = self.RetrievePageOfNicknames()
  269.         return self.AddAllElementsFromAllPages(ret, gdata.apps.NicknameFeedFromString)
  270.  
  271.     
  272.     def RetrieveNicknames(self, user_name):
  273.         '''Retrieve nicknames of the user'''
  274.         uri = '%s/nickname/%s?username=%s' % (self._baseURL(), API_VER, user_name)
  275.         
  276.         try:
  277.             ret = gdata.apps.NicknameFeedFromString(str(self.Get(uri)))
  278.         except gdata.service.RequestError:
  279.             e = None
  280.             raise AppsForYourDomainException(e.args[0])
  281.  
  282.         return self.AddAllElementsFromAllPages(ret, gdata.apps.NicknameFeedFromString)
  283.  
  284.     
  285.     def RetrieveNickname(self, nickname):
  286.         '''Retrieve a nickname.
  287.  
  288.     Args:
  289.       nickname: string The nickname to retrieve
  290.  
  291.     Returns:
  292.       gdata.apps.NicknameEntry
  293.     '''
  294.         uri = '%s/nickname/%s/%s' % (self._baseURL(), API_VER, nickname)
  295.         
  296.         try:
  297.             return gdata.apps.NicknameEntryFromString(str(self.Get(uri)))
  298.         except gdata.service.RequestError:
  299.             e = None
  300.             raise AppsForYourDomainException(e.args[0])
  301.  
  302.  
  303.     
  304.     def CreateNickname(self, user_name, nickname):
  305.         '''Create a nickname'''
  306.         uri = '%s/nickname/%s' % (self._baseURL(), API_VER)
  307.         nickname_entry = gdata.apps.NicknameEntry()
  308.         nickname_entry.login = gdata.apps.Login(user_name = user_name)
  309.         nickname_entry.nickname = gdata.apps.Nickname(name = nickname)
  310.         
  311.         try:
  312.             return gdata.apps.NicknameEntryFromString(str(self.Post(nickname_entry, uri)))
  313.         except gdata.service.RequestError:
  314.             e = None
  315.             raise AppsForYourDomainException(e.args[0])
  316.  
  317.  
  318.     
  319.     def DeleteUser(self, user_name):
  320.         '''Delete a user account'''
  321.         uri = '%s/user/%s/%s' % (self._baseURL(), API_VER, user_name)
  322.         
  323.         try:
  324.             return self.Delete(uri)
  325.         except gdata.service.RequestError:
  326.             e = None
  327.             raise AppsForYourDomainException(e.args[0])
  328.  
  329.  
  330.     
  331.     def UpdateUser(self, user_name, user_entry):
  332.         '''Update a user account.'''
  333.         uri = '%s/user/%s/%s' % (self._baseURL(), API_VER, user_name)
  334.         
  335.         try:
  336.             return gdata.apps.UserEntryFromString(str(self.Put(user_entry, uri)))
  337.         except gdata.service.RequestError:
  338.             e = None
  339.             raise AppsForYourDomainException(e.args[0])
  340.  
  341.  
  342.     
  343.     def CreateUser(self, user_name, family_name, given_name, password, suspended = 'false', quota_limit = None, password_hash_function = None):
  344.         '''Create a user account. '''
  345.         uri = '%s/user/%s' % (self._baseURL(), API_VER)
  346.         user_entry = gdata.apps.UserEntry()
  347.         user_entry.login = gdata.apps.Login(user_name = user_name, password = password, suspended = suspended, hash_function_name = password_hash_function)
  348.         user_entry.name = gdata.apps.Name(family_name = family_name, given_name = given_name)
  349.         if quota_limit is not None:
  350.             user_entry.quota = gdata.apps.Quota(limit = str(quota_limit))
  351.         
  352.         
  353.         try:
  354.             return gdata.apps.UserEntryFromString(str(self.Post(user_entry, uri)))
  355.         except gdata.service.RequestError:
  356.             e = None
  357.             raise AppsForYourDomainException(e.args[0])
  358.  
  359.  
  360.     
  361.     def SuspendUser(self, user_name):
  362.         user_entry = self.RetrieveUser(user_name)
  363.         if user_entry.login.suspended != 'true':
  364.             user_entry.login.suspended = 'true'
  365.             user_entry = self.UpdateUser(user_name, user_entry)
  366.         
  367.         return user_entry
  368.  
  369.     
  370.     def RestoreUser(self, user_name):
  371.         user_entry = self.RetrieveUser(user_name)
  372.         if user_entry.login.suspended != 'false':
  373.             user_entry.login.suspended = 'false'
  374.             user_entry = self.UpdateUser(user_name, user_entry)
  375.         
  376.         return user_entry
  377.  
  378.     
  379.     def RetrieveUser(self, user_name):
  380.         '''Retrieve an user account.
  381.  
  382.     Args:
  383.       user_name: string The user name to retrieve
  384.  
  385.     Returns:
  386.       gdata.apps.UserEntry
  387.     '''
  388.         uri = '%s/user/%s/%s' % (self._baseURL(), API_VER, user_name)
  389.         
  390.         try:
  391.             return gdata.apps.UserEntryFromString(str(self.Get(uri)))
  392.         except gdata.service.RequestError:
  393.             e = None
  394.             raise AppsForYourDomainException(e.args[0])
  395.  
  396.  
  397.     
  398.     def RetrievePageOfUsers(self, start_username = None):
  399.         '''Retrieve one page of users in this domain.'''
  400.         uri = '%s/user/%s' % (self._baseURL(), API_VER)
  401.         if start_username is not None:
  402.             uri += '?startUsername=%s' % start_username
  403.         
  404.         
  405.         try:
  406.             return gdata.apps.UserFeedFromString(str(self.Get(uri)))
  407.         except gdata.service.RequestError:
  408.             e = None
  409.             raise AppsForYourDomainException(e.args[0])
  410.  
  411.  
  412.     
  413.     def GetGeneratorForAllUsers(self):
  414.         '''Retrieve a generator for all users in this domain.'''
  415.         first_page = self.RetrievePageOfUsers()
  416.         return self.GetGeneratorFromLinkFinder(first_page, gdata.apps.UserFeedFromString)
  417.  
  418.     
  419.     def RetrieveAllUsers(self):
  420.         '''Retrieve all users in this domain. OBSOLETE'''
  421.         ret = self.RetrievePageOfUsers()
  422.         return self.AddAllElementsFromAllPages(ret, gdata.apps.UserFeedFromString)
  423.  
  424.  
  425.  
  426. class PropertyService(gdata.service.GDataService):
  427.     '''Client for the Google Apps Property service.'''
  428.     
  429.     def __init__(self, email = None, password = None, domain = None, source = None, server = 'apps-apis.google.com', additional_headers = None):
  430.         gdata.service.GDataService.__init__(self, email = email, password = password, service = 'apps', source = source, server = server, additional_headers = additional_headers)
  431.         self.ssl = True
  432.         self.port = 443
  433.         self.domain = domain
  434.  
  435.     
  436.     def _GetPropertyEntry(self, properties):
  437.         property_entry = gdata.apps.PropertyEntry()
  438.         property = []
  439.         for name, value in properties.iteritems():
  440.             if name is not None and value is not None:
  441.                 property.append(gdata.apps.Property(name = name, value = value))
  442.                 continue
  443.         
  444.         property_entry.property = property
  445.         return property_entry
  446.  
  447.     
  448.     def _PropertyEntry2Dict(self, property_entry):
  449.         properties = { }
  450.         for i, property in enumerate(property_entry.property):
  451.             properties[property.name] = property.value
  452.         
  453.         return properties
  454.  
  455.     
  456.     def _GetProperties(self, uri):
  457.         
  458.         try:
  459.             return self._PropertyEntry2Dict(gdata.apps.PropertyEntryFromString(str(self.Get(uri))))
  460.         except gdata.service.RequestError:
  461.             e = None
  462.             raise gdata.apps.service.AppsForYourDomainException(e.args[0])
  463.  
  464.  
  465.     
  466.     def _PostProperties(self, uri, properties):
  467.         property_entry = self._GetPropertyEntry(properties)
  468.         
  469.         try:
  470.             return self._PropertyEntry2Dict(gdata.apps.PropertyEntryFromString(str(self.Post(property_entry, uri))))
  471.         except gdata.service.RequestError:
  472.             e = None
  473.             raise gdata.apps.service.AppsForYourDomainException(e.args[0])
  474.  
  475.  
  476.     
  477.     def _PutProperties(self, uri, properties):
  478.         property_entry = self._GetPropertyEntry(properties)
  479.         
  480.         try:
  481.             return self._PropertyEntry2Dict(gdata.apps.PropertyEntryFromString(str(self.Put(property_entry, uri))))
  482.         except gdata.service.RequestError:
  483.             e = None
  484.             raise gdata.apps.service.AppsForYourDomainException(e.args[0])
  485.  
  486.  
  487.  
  488.